<?php
namespace app\index\controller;

use app\common\model\Article as M_Article;
use bigDream\thinkJump\Jump;
use think\facade\Request;

/**
 * Class Article
 * @package app\index\controller
 * @author jwj <jwjbjg@gmail.com>
 * @copyright jwj
 */
class Article
{
    public function index()
    {
        $query = M_Article::field(['id', 'title', 'create_time'])
            ->where('status', 1)
            ->order('update_time', 'desc');

        if (Request::has('category_id', 'param', true)) {
            $query->where('category_id', Request::param('category_id'));
        }

        $result = $query->paginate(Request::param('limit', 20));

        if (Request::isGet()) {
            return view('', ['result' => $result]);
        }
    }

    public function info()
    {
        $info = M_Article::where('id', Request::param('id/d'))->where('status', 1)->find();
        if (null === $info) Jump::error('内容不存在');

        
        M_Article::where('id', $info['id'])->inc('view_count')->update();

        $new = M_Article::where('category_id', $info->category_id)
            ->where('status', 1)
            ->where('id', '<>', $info->id)
            ->order('update_time', 'desc')
            ->field(['id', 'title'])
            ->limit(5)
            ->cache(600)
            ->select();

        $hot = M_Article::where('category_id', $info->category_id)
            ->where('status', 1)
            ->where('id', '<>', $info->id)
            ->order('view_count', 'desc')
            ->field(['id', 'title'])
            ->limit(5)
            ->cache(600)
            ->select();

        return view('', [
            'info' => $info,
            'new'  => $new,
            'hot'  => $hot,
        ]);
    }
}